--
-- Author: shineflag
-- Date: 2016-09-22 10:19:53
--


--协议文本
local protos = {
	--注册
	"signup" = {
		req = {
			userid = "",
		},
		resp = {
			ret = 0,
		}
	},
	--登录
	"signin" = {
		req = {
			userid = "",
		},
		resp = {
			ret = 0,
		}
	},
}

local function pack(data)
    return json.encode(data)
end

local function unpack(msg)
	local info = json.decode(msg)
	return info
end



--创建一个请求的包
local function create_req(cmd,args)

	local p = protos[cmd]
	if p then
		local data = {}
		data.cmd = cmd 
		if p.req then
			local req = {}
			for key,value in pairs(p.req)
				if args[key] then
					req[key] = args[key]
				else
					req[key] = value
				end
			end

			data.req = req
		end

		return pack(data)
	end

	return nil 

end

--根据参数创建一个回复包
local function create_resp(cmd,args)

	local p = protos[cmd]
	if p then
		local data = {}
		data.cmd = cmd 
		if p.resp then
			local resp = {}
			for key,value in pairs(p.resp)
				if args[key] then
					resp[key] = args[key]
				else
					resp[key] = value
				end
			end

			data.resp = resp
		end

		return pack(data)
	end

	return nil 

end

local proto = {}
proto.create_req = create_req
proto.create_resp = create_resp
proto.unpack = unpack

return proto 